package com.garen.orm;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import java.util.Properties;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

public class TestUtils {
	protected static Log log = LogFactory.getLog(TestUtils.class);
	private int count = 0;
	private Map<String,String> map = new HashMap<>();
	
	private Map<String,String> enMap = new HashMap<>();
	
	public Map<String,String> test(){
		String basePath = "D:\\javadev\\20150701\\apptest\\src\\properties";
		bfile(new File(basePath));
		
		return map;
	}
	
	public Map<String,String> test1(){
		//String basePath = "D:\\javadev\\20150701\\apptest\\src\\properties";
		//bfile(new File(basePath));
		return enMap;
	}
	
	//sql生成page语句
	public static String createPageSql(String sql){
		String [] sqls = sql.split("\\s+");
		int flag = 0;
		StringBuilder newSql = new StringBuilder("select count(1) from");
		for(String s :sqls){
			if(flag == -1){
				newSql.append(" " + s);
			}else{
				if("select".equals(s) || "(select".equals(s)) flag++;
				else if("from".equals(s)) {
					if(1 == flag){
						flag = -1;
					}else flag--;
				}
			}
		}
		return newSql.toString();
	}
	
	//1 中文 2 英文 0 未知
	public static int checkFile(String fileName){
		if(fileName.indexOf("zh_CN") != -1 ) return 1;
		if(fileName.indexOf("en_US") != -1 ) return 2;
		return 0;
	}
		
	public static void main(String[] args) {
		//String sql = "select distinct Class,right('00'+cast(datepart(hh,dateadd(second,sksj,'')) as varchar),2) as kssj1,  right('00'+cast(datepart(n,dateadd(second,sksj,'')) as varchar),2) as kssj2,  right('00'+cast(datepart(hh,dateadd(second,Xksj,'')) as varchar),2) as jssj1,  right('00'+cast(datepart(n,dateadd(second,Xksj,'')) as varchar),2) as jssj2 from tkq_shid   where parent_bh= :parentXh and convert(varchar(10),class_sj,120)>=:kssj   and convert(varchar(10),class_sj,120)<=:jssj order by Class asc ";
		//sql = createPageSql(sql);
		//print(sql);
		testMap();
	}
	static void testMap(){
		
	}
	public  void bfile(File file){
		File[] files = file.listFiles();
		for(File f : files){
			if(f.isDirectory()) bfile(f);
			else{
				int type = checkFile(f.getName());
				if(type == 1) doFile1(f);
				else if(type == 2) doFile2(f);
			}
		}
	}
	
	private void doFile1(File file){
		Properties prop = new Properties();
		print(file.getName());
		try {
			prop.load(new FileInputStream(file));
			for(Object key : prop.keySet()){
				//print("\t" + prop.getProperty(key.toString()));
				map.put(prop.getProperty(key.toString()), key.toString());
				count++;
			}
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	

	private void doFile2(File file){
		Properties prop = new Properties();
		print(file.getName());
		try {
			prop.load(new FileInputStream(file));
			for(Object key : prop.keySet()){
				//print("\t" + prop.getProperty(key.toString()));
				enMap.put(key.toString(),prop.getProperty(key.toString()));
				count++;
			}
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	
	static void print(Object msg){
		System.out.println(msg.toString());
	}
}
